package com.gqz.springbootdemo.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.gqz.springbootdemo.mapper.MenuDao;
import com.gqz.springbootdemo.pojo.MenuPO;
import com.gqz.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-05-11 20:56:11
 */
@RestController
@RequestMapping("user")
public class UserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Autowired
    private MenuDao menuDao;

    @GetMapping("a")
    public void test(@RequestParam("parentId") Long parentId,
                     @RequestParam("orgName") String orgName,
                     @RequestParam("orgId") Long orgId,
                     @RequestParam("deptId") Long deptId) {
        MenuPO familyMenu = getFamilyMenu(parentId);
        logger.info(JSONUtil.toJsonPrettyStr(familyMenu));
        //遍历菜单树，在原有名称的基础上加上OrgName
        updateMenu(familyMenu, orgName,orgId,deptId);
    }

    /**
     * 水下
     */
    @GetMapping("b")
    public void test2() {
        LambdaQueryWrapper<MenuPO> wrapper = Wrappers
                .lambdaQuery(MenuPO.class)
                .eq(MenuPO::getParentId, -1)
                .likeRight(MenuPO::getId,"173");
        List<MenuPO> menuPOS = menuDao.selectList(wrapper);
        logger.info(JSONUtil.toJsonPrettyStr(menuPOS));
        for (MenuPO po : menuPOS) {
            test(po.getId(), "水下", 1430192324638408705L, 1430192324638408705L);
        }
    }

    private void updateMenu(MenuPO menuPO, String orgName, Long orgId, Long deptId) {
        menuPO.setName(updateName(menuPO.getName(), orgName));
        menuPO.setMainName(updateName(menuPO.getMainName(), orgName));
        menuPO.setOrgId(orgId);
        menuPO.setDeptId(deptId);
        menuPO.setId(IdUtil.getSnowflakeNextId());
        menuDao.insert(menuPO);
        List<MenuPO> children = menuPO.getChildren();
        if (children != null && !children.isEmpty()) {
            for (MenuPO child : children) {
                //更新父ID
                child.setParentId(menuPO.getId());
                updateMenu(child, orgName,orgId,deptId);
            }
        }
    }

    private String updateName(String name, String orgName) {
        String[] split = name.split("-");
        if (split.length == 1 || split[1].contains("计量")) {
            return name + "-" + orgName;
        }else {
            return split[0] + "-" + orgName;
        }
    }

    private MenuPO getFamilyMenu(Long parentId) {
        MenuPO menuPO = menuDao.selectById(parentId);

        LambdaQueryWrapper<MenuPO> wrapper = Wrappers.lambdaQuery(MenuPO.class).eq(MenuPO::getParentId, parentId);
        List<MenuPO> menuPOS = menuDao.selectList(wrapper);
        menuPO.setChildren(menuPOS);
        for (MenuPO po : menuPOS) {
            MenuPO familyMenu = getFamilyMenu(po.getId());
            po.setChildren(familyMenu.getChildren());
        }
        return menuPO;
    }


}

